<?php
	/** Include path **/
	//set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
	/** Include PHPExcel */
	require_once '../Classes/PHPExcel.php';

	if(!extension_loaded("soap"))
	{  

	  dl("php_soap.dll");  

	}  

	   

	ini_set("soap.wsdl_cache_enabled","0");  

	$server = new SoapServer("excelWS.wsdl");  
	
	

	function browserOutput($browserData)
	{  
		//copy('Templates/BrowserOutput.xlsx','Temp/BrowserOutput.xlsx');
		
		$start=time();
		
		$dataTable=array();
		$DataRows=explode("#NEWLINE#", $browserData);
		$rowCount=count($DataRows)-1;
		for($i=0;$i<$rowCount;$i++)
		{
			$value = str_replace("&amp;",  "&", $DataRows[$i]);
			$value = str_replace("&lt;",   "<", $value);
			$value = str_replace("&gt;",   ">", $value);
			$value = str_replace("#quot#", "\"", $value);
			$value = str_replace("&apos;", "\'", $value);
			$value = str_replace("#back#", "\\", $value);
			$Coumun=explode("\t", $value);
			array_push($dataTable,$Coumun);
		}
		
		
		//$browserData=utf8_decode($browserData);
		
		
		$inputFileName = './Templates/BrowserOutput.xlsx';

		/** Load $inputFileName to a PHPExcel Object  **/
		$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
		/*$objPHPExcel->getProperties()->setCreator("Teamcenter")
							 ->setLastModifiedBy("Teamcenter")
							 ->setTitle("XLSX Browser Output")
							 ->setSubject("PHPExcel Test Document")
							 ->setDescription("Test document for PHPExcel, generated using PHP classes.")
							 ->setKeywords("office XLSX Browser output")
							 ->setCategory("Test result file");
		*/
		$objPHPExcel->setActiveSheetIndex(0);
		
		$sheet=$objPHPExcel->getActiveSheet();
		$i=1;
		$alpha=65;
		$max=0;
		$alpha1=64;
		$pub1='A';
		
		
		
		$colCount=count($dataTable[0]);
		
		for($i=0;$i<$colCount;$i++)
		{
			for($j=0;$j<$rowCount;$j++)
			{
				if($alpha>90)
				{
					
					$alpha1++;
					$alpha=65;
				}
				if($alpha1<65)
				{
					
					$pub1=sprintf("%c",$alpha);
					$cellID=sprintf("%c%d",$alpha,$j+1);
					
				}
				else
				{
				
					$pub1=sprintf("%c%c",$alpha1,$alpha);
					$cellID=sprintf("%c%c%d",$alpha1,$alpha,$j+1);
					
				}
				if(strlen($dataTable[$j][$i])>$max)
						$max=strlen($dataTable[$j][$i]);
				$cellData=$dataTable[$j][$i];
				if($cellData[0]=='+' || $cellData[0]=='-' || $cellData[0]=='=' || $cellData[0]=='/' || $cellData[0]=='*')
					$cellData=" ".$cellData;
				$sheet->setCellValue($cellID,$cellData);
				
			}
			if($max>40)
			{
				$max=40;
			}
			
			$sheet->getColumnDimension($pub1)->setWidth($max+2);
			
			$max=0;
			$alpha++;
		}
		
		//return ($rowCount);
		//write data to the sheet
		//$sheet->fromArray($dataTable, NULL, 'A1');
		
		//freeze the sheet
		$sheet->freezePane('B2');
		
		//set auto filter
		$sheet->setAutoFilter($objPHPExcel->getActiveSheet()->calculateWorksheetDimension());

		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);
		
		
		
		$fname="BrowserOutput_".time().".xlsx";
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save('./Temp/'.$fname);
		//return(time()-$start);
		return "!ServerDownload!/".$fname;  
		
	}  

	   

	$server->AddFunction("browserOutput");  

	$server->handle();  



?>